//
//  HBRepostView.swift
//  sinaWeibo
//
//  Created by Beans on 16/6/27.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit
import SnapKit

class HBRepostView: UIView {

    // (1) 定义属性
    var bottomConstraint: Constraint?

    var repostViewModel: HBStatusCellViewModel? {
        didSet {
//            contentLabel.text = repostViewModel?.repostContent
            contentLabel.attributedText = repostViewModel?.repostAttributeString

            // (3) 卸载约束
            bottomConstraint?.uninstall()

            if repostViewModel?.model?.retweeted_status?.pic_urls?.count > 0 {
                printLog("显示转发配图")
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    // (4) 更新约束
                    self.bottomConstraint = make.bottom.equalTo(pictureView).constraint
                })

            } else {
                printLog("隐藏转发配图")
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    // (4)
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).constraint
                })
            }
        }
    }

    // 1. 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 2. 添加并布局子控件
    private func setupUI() {

        backgroundColor = UIColor(white: 0.95, alpha: 1)

        addSubview(contentLabel)
        addSubview(pictureView)

        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            make.top.equalTo(self.snp_top).offset(HomeCellMargin * 0.5)
            make.right.equalTo(self.snp_right).offset(-HomeCellMargin)
//            make.bottom.equalTo(pictureView.snp_top).offset(-HomeCellMargin * 0.5)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin * 0.5)
            make.left.equalTo(self).offset(HomeCellMargin)
            // temp
            make.width.height.equalTo(100)
        }
        // 自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            // (2) 设置约束
            self.bottomConstraint = make.bottom.equalTo(pictureView).constraint
        }

    }

    // 3. lazy load
    // 转发内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.text = "这里是转发内容"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15.0)
        label.numberOfLines = 0
        return label
    }()

    private lazy var pictureView: HBPictureView = HBPictureView()

}
